﻿<cfsilent>
	<cfscript>
		
		stringAdvice = getProperty("serviceFactory").getBean("stringAdvice");
		dateTimeAdvice = getProperty("serviceFactory").getBean("datetimeAdvice");
		
		event.setArg("pageTitle", "学生调查问卷 - 调查问卷库");

		currentTab = event.getArg("TabID", "labCreateVote");
		
		sql = "	SELECT
					DISTINCT clz.grade
				FROM
					t_class clz INNER JOIN t_subject sbj ON sbj.sbj_id = clz.sbj_id
				WHERE
					clz.cls_scale > 0
					AND
					clz.grade + sbj.schooling_length > :yearNow 
				ORDER BY
					clz.grade";
		
		queryObj = new Query(datasource = application.dnsSlave);
		queryObj.addParam(name = "yearNow", value = year(now()), cfsqltype = "cf_sql_char");
		rs_grade = queryObj.execute(sql = sql).getResult();

		sql = "	SELECT
					DISTINCT i.institute_id,i.institute_short
				FROM
					t_class clz INNER JOIN t_subject sbj ON clz.sbj_id = sbj.sbj_id INNER JOIN t_institute i ON i.institute_id = sbj.institute_id
				WHERE
					clz.cls_scale > 0
					AND
					clz.grade + sbj.schooling_length > :yearNow 
				ORDER BY
					i.institute_short";
		
		queryObj = new Query(datasource = application.dnsSlave);
		queryObj.addParam(name = "yearNow", value = year(now()), cfsqltype = "cf_sql_char");
		rs_institute = queryObj.execute(sql = sql).getResult();

		sql = "	SELECT
					q.qsn_id,q.title,q.describe,q.date_start,q.date_end
				FROM
					t_questionnaire q
				WHERE
					q.date_start >= :start 
					AND
					q.date_start <= :end 
					AND
					q.state = '2'
				ORDER BY
					q.date_end,q.title";
		
		queryObj = new Query(datasource = application.dnsSlave);
		queryObj.addParam(name = "start", value = dateFormat(now(), "YYYYMM01"), cfsqltype = "cf_sql_char");
		queryObj.addParam(name = "end", value = dateFormat(dateAdd("m", 1, now()), "YYYYMM31"), cfsqltype = "cf_sql_char");
		rs_questRecentMonth = queryObj.execute(sql = sql).getResult();
		
		sql = "	SELECT 
					q.qsn_id,q.title,q.describe,q.date_start,q.date_end
				FROM
					t_questionnaire q
				WHERE
					q.date_start >= :start 
					AND
					q.date_start <= :end 
					AND
					q.state = '2'
				ORDER BY
					q.date_end,q.title";
					
		queryObj = new Query(datasource = application.dnsSlave);
		queryObj.addParam(name = "start", value = dateFormat(dateAdd("m", -2, now()), "YYYYMM01"), cfsqltype = "cf_sql_char");
		queryObj.addParam(name = "end", value = dateFormat(dateAdd("m", -1, now()), "YYYYMM31"), cfsqltype = "cf_sql_char");
		rs_questRecent3Month = queryObj.execute(sql = sql).getResult();

		sql = "	SELECT
					a.qsn_id,
					a.title,
					a.date_start,
					a.date_end,
					t.tch_name,
					(SELECT COUNT(*) FROM t_respondents b WHERE a.qsn_id = b.qsn_id AND b.state = 'S') count_1,
					(SELECT COUNT(*) FROM t_respondents c WHERE a.qsn_id = c.qsn_id) count_2
				FROM
					t_questionnaire a INNER JOIN t_teacher t ON a.tch_id = t.tch_id
				WHERE
					a.date_start <= :today 
					AND
					a.date_end >= :today 
					AND
					a.state = '2'
				ORDER BY
					a.title";
		
		queryObj = new Query(datasource = application.dnsSlave);
		queryObj.addParam(name = "today", value = dateFormat(now(), "YYYYMMDD"), cfsqltype = "cf_sql_char");
		rs_questDueDate = queryObj.execute(sql = sql).getResult();
		
		sql = "	SELECT
					a.qsn_id,
					a.title,
					a.date_start,
					a.date_end,
					t.tch_name
				FROM
					t_questionnaire a INNER JOIN t_teacher t ON a.tch_id = t.tch_id
				WHERE
					a.state = '1'
				ORDER BY
					a.title";
		
		queryObj = new Query(datasource = application.dnsSlave);
		rs_questWait = queryObj.execute(sql = sql).getResult();
		
		questCount = rs_questRecentMonth.recordCount + rs_questRecent3Month.recordCount;
		
		if ( questCount ) {
			currentTab = event.getArg("TabID", "labVote");
		}
		
		if ( rs_questDueDate.recordCount ) {
			currentTab = event.getArg("TabID", "labRunning");
		}
		
		passArgs = structNew();
		
	</cfscript>
</cfsilent>

<div id="headArea">
	<div class="uiHeader uiHeaderWithImage ptm">
		<div class="clearfix uiHeaderTop">
            <div class="uiHeaderActions rfloat">
				<a class="uiButton" href="<cfoutput>#buildURL('DEFAULT_EVENT')#</cfoutput>"><i class="mrs img btnPublish"></i><span class="uiButtonText">在线调查</span></a>
			</div>
			<div>
				<h2 class="uiHeaderTitle">
					<i class="uiHeaderImage img icon16x16 card"></i>调查问卷库
				</h2>
			</div>
		</div>
	</div>
</div>

<div class="UIElement">
	<ul class="senateFirstPage">
		<li>
			<div class="clearfix" id="summary">
				<div class="UItab">
					<div class="tabLables">
						<cfif rs_questDueDate.recordCount>
							<span <cfif currentTab eq "labRunning">class="active"</cfif> id="labRunning" tabTarget="Running">进行中的问卷</span>
						</cfif>
						<cfif questCount>
							<span <cfif currentTab eq "labVote">class="active"</cfif> id="labVote" tabTarget="Vote">调查问卷</span>
						</cfif>
						<cfif rs_questWait.recordCount>
							<span <cfif currentTab eq "labWait">class="active"</cfif> id="labWait" tabTarget="Wait">未发布</span>
						</cfif>
						<span <cfif currentTab eq "labCreateVote">class="active"</cfif> id="labCreateVote" tabTarget="CreateVote">创建调查问卷</span>
					</div>
				</div>
				<script language="javascript" type="text/javascript">
					//<![CDATA[
					<!--
					$(document).ready(function() {
						
						/* init Tab */
						$("div.tabContent").hide();
						$("div#" + $("div.tabLables span.active").attr("tabTarget")).show();
						
						$("div.tabLables > span").click(function(){
							var $targetID = $(this).attr("tabTarget");
							
							$("div.tabLables span").removeClass("active");
							$(this).addClass("active");
							
							$("div.tabContent").not('[id="' + $targetID + '"]').hide();
							$("div.tabContent#" + $targetID).show();
						});
						
					});
					// -->
					//]]>				
				</script>
				
				<cfif rs_questDueDate.recordCount>
					<div id="Running" class="tabContent">
						
						<div class="noticeBlock">
							<h3>正在进行的问卷</h3>
							<p><span class="img icon16x16 message"></span>目前正在进行调查数据收集的问卷, 在这里您可以查看进行中问卷的数据收集状况.</p>
						</div>
						
						<cfset row = 0 />
						<table class="UIEditable">
							<thead>
								<tr>
									<td width="30"></td>
									<td>问卷</td>
									<td>发布人</td>
									<td width="90">起止时间</td>
									<td width="40">回执</td>
									<td width="40">预计</td>
									<td width="18"></td>
								</tr>
							</thead>
							<tbody>
                                <cfloop query="rs_questDueDate">
									<cfset row++ />
									<cfset structInsert( passArgs, "QSN", rs_questDueDate.qsn_id, true ) />
									
									<tr class="editRows">
										<td class="index"><cfoutput>#row#</cfoutput></td>
										<td><cfoutput>#rs_questDueDate.title#</cfoutput></td>
										<td><cfoutput>#rs_questDueDate.tch_name#</cfoutput></td>
										<td><span class="numeric"><cfoutput>#dateTimeAdvice.formatDateString( rs_questDueDate.date_start, 'MM/DD' )#</cfoutput> - <cfoutput>#dateTimeAdvice.formatDateString( rs_questDueDate.date_end, 'MM/DD' )#</cfoutput></span></td>
										<td><span class="numeric"><cfoutput>#rs_questDueDate.count_1#</cfoutput></span></td>
										<td><span class="numeric"><cfoutput>#rs_questDueDate.count_2#</cfoutput></span></td>
										<td><a href="<cfoutput>#buildURL('voteDetail', passArgs)#</cfoutput>" class="stat details"></a></td>
									</tr>
									
								</cfloop>
								<cfset structDelete( passArgs, "QSN", false ) />
							</tbody>
						</table>
						
					</div>
				</cfif>
				
				<cfif questCount>
					
					<div id="Vote" class="tabContent">
						
						<div class="noticeBlock">
							<h3>近期调查问卷</h3>
							<p><span class="img icon16x16 message"></span>系统最近三个月发布的调查问卷汇总表, 选中问卷可查看调查结果</p>
						</div>
						
						<cfif rs_questRecentMonth.recordCount>
							<div class="noticeBlock">
								<h2>近一个月发布</h2>
								<p>这里仅列出最近的 <em>12</em> 份问卷, 如需查看详细的问卷信息, 请从上方问卷分类标签查看详细情况</p>
								<hr/>
							</div>
							
							<div class="clearfix">
								<div class="UICardTable">
                                    <cfloop query="rs_questRecentMonth">
										<cfset structInsert( passArgs, "QSN", rs_questRecentMonth.qsn_id, true ) />
										<dl>
											<dt>
												<a href="<cfoutput>#buildURL('voteDetail', passArgs)#</cfoutput>" title="<cfoutput>#stringAdvice.HTMLFormEncode( rs_questRecentMonth.describe )#</cfoutput>">
													<em class="questionnaire"><!--imgholder--></em>
													<dd>
														<h3 class="fixed"><cfoutput>#stringAdvice.XHTMLSafeFormat( rs_questRecentMonth.title )#</cfoutput></h3>
														<div>时限:<span><cfoutput>#dateTimeAdvice.formatDateString( rs_questRecentMonth.date_start, 'YYYY/MM/DD' )#</cfoutput> - <cfoutput>#dateTimeAdvice.formatDateString( rs_questRecentMonth.date_end, 'YYYY/MM/DD' )#</cfoutput></span></div>
													</dd>
												</a>
											</dt>
										</dl>
									</cfloop>
									<cfset structDelete( passArgs, "QSN", false ) />
								</div>
							</div>
							
						</cfif>
						
						<cfif rs_questRecent3Month.recordCount>
							<div class="noticeBlock">
								<h2>最近三个月发布</h2>
								<p>这里仅列出最近的 <em>12</em> 份问卷, 如需查看详细的问卷信息, 请从上方问卷分类标签查看详细情况</p>
								<hr/>
							</div>
							
							<div class="clearfix">
								<div class="UICardTable">
                                    <cfloop query="rs_questRecent3Month">
										<cfset structInsert(passArgs, "QSN", rs_questRecent3Month.qsn_id, true) />
										<dl>
											<dt>
												<a href="<cfoutput>#buildURL('voteDetail', passArgs)#</cfoutput>" title="<cfoutput>#stringAdvice.HTMLFormEncode( rs_questRecent3Month.describe )#</cfoutput>">
													<em class="questionnaire"><!--imgholder--></em>
													<dd>
														<h3><cfoutput>#stringAdvice.XHTMLSafeFormat( rs_questRecent3Month.title )#</cfoutput></h3>
														<div>时限:<span><cfoutput>#dateTimeAdvice.formatDateString( rs_questRecent3Month.date_start, 'YYYY/MM/DD' )#</cfoutput> - <cfoutput>#dateTimeAdvice.formatDateString( rs_questRecent3Month.date_end, 'YYYY/MM/DD' )#</cfoutput></span></div>
													</dd>
												</a>
											</dt>
										</dl>
									</cfloop>
									<cfset structDelete( passArgs, "QSN", false ) />
								</div>
							</div>
							
						</cfif>
						
					</div>
					
				</cfif>
				
				<cfif rs_questWait.recordCount>
					
					<div id="Wait" class="tabContent">
						
						<div class="noticeBlock">
							<h3>未发布的问卷</h3>
							<p><span class="img icon16x16 message"></span>目前正在进行编辑或尚未向学生发布的调查问卷</p>
						</div>
						
						<cfset row = 0 />
						<table class="UIEditable">
							<thead>
								<tr>
									<td width="30"></td>
									<td>问卷</td>
									<td>发布人</td>
									<td width="80">起止时间</td>
									<td width="18"></td>
								</tr>
							</thead>
							<tbody>
                                <cfloop query="rs_questWait">
									<cfset row++ />
									<cfset structInsert( passArgs, "QSN", rs_questWait.qsn_id, true ) />
									
									<tr class="editRows">
										<td class="index"><cfoutput>#row#</cfoutput></td>
										<td><cfoutput>#rs_questWait.title#</cfoutput></td>
										<td><cfoutput>#rs_questWait.tch_name#</cfoutput></td>
										<td><span class="numeric"><cfoutput>#dateTimeAdvice.formatDateString( rs_questWait.date_start, 'MM/DD' )#</cfoutput> - <cfoutput>#dateTimeAdvice.formatDateString( rs_questWait.date_end, 'MM/DD' )#</cfoutput></span></td>
										<td><a href="<cfoutput>#buildURL('voteSetupWizard', passArgs)#</cfoutput>" class="edit"></a></td>
									</tr>
									
								</cfloop>
								<cfset structDelete( passArgs, "QSN", false ) />
							</tbody>
						</table>
						
					</div>
				
				</cfif>
				
				<div id="CreateVote" class="tabContent">
					
					<div class="noticeBlock">
						<h3>建立新的调查问卷</h3>
						<p>
							<span class="img icon16x16 message"></span> 若需建立和发布新的在线调查问卷, 请根据实际需要完成下列表格
						</p>
					</div>
					
					<form id="formCreate" onSubmit="javascript:return processVerfiyForm('formCreate');" class="formWrapper" method="post" action="<cfoutput>#buildURL('voteCreateDo')#</cfoutput>">
						
						<div class="label">
							<b>标题</b>
							<input name="Title" id="Title" verification="required" type="text" class="typeText" maxlength="180" value="<cfoutput>#event.getArg('Title')#</cfoutput>" />
						</div>
						
						<div class="label">
							<b>问卷调查说明</b>
							<textarea name="Describe" id="Describe" verification="required"><cfoutput>#event.getArg('Describe')#</cfoutput></textarea>
						</div>
						
						<div class="label">
							<b>起止时间</b>
							<input name="DateStart" id="DateStart" type="text" class="typeDate" verification="date" maxlength="10" value="<cfoutput>#dateFormat( dateAdd('d', 1, now()), 'YYYY-MM-DD')#</cfoutput>" /><span id="startTrigger" class="btnCalendar"></span>
							&nbsp;&nbsp;
							至
							&nbsp;&nbsp;
							<input name="DateEnd" id="DateEnd" type="text" class="typeDate" verification="date" maxlength="10" value="<cfoutput>#dateFormat( dateAdd('d', 30, now()), 'YYYY-MM-DD')#</cfoutput>" /><span id="endTrigger" class="btnCalendar"></span>
							<p class="notice">该起止时间用于设置在线调查问卷的有效填写时间, 默认从建立后的第二天开始, 有效时间为 14 天. 若该时间设置不符合您的要求, 请自行更正.</p>
						</div>
						
						<hr/>
						
						<div class="label">
							<b>投票范围</b>
							
							<input name="FullGrade" id="FullGrade" type="checkbox" class="typeCheckbox" value="1" checked="checked" /> 所有年级
							&nbsp;&nbsp;&nbsp;&nbsp;
							<input name="FullInstitute" id="FullInstitute" type="checkbox" class="typeCheckbox" value="1" checked="checked" /> 所有学院
							&nbsp;&nbsp;&nbsp;&nbsp;
							<input name="FullGender" id="FullGender" type="checkbox" class="typeCheckbox" value="1" checked="checked" /> 不限性别
							&nbsp;&nbsp;&nbsp;&nbsp;
							<input name="SpecialStudent" id="SpecialStudent" type="checkbox" class="typeCheckbox" value="1" /> 特定学生
							
							<div id="notFullGrade" class="secondRow hidden_elem mtm">
								指定年级
								<cfset row = 0 />
                                <cfloop query="rs_grade">
									&nbsp;&nbsp;
									<input name="targetGrade" type="checkbox" class="typeCheckbox" <cfif row eq 1>verification="group"</cfif> value="<cfoutput>#rs_grade.grade#</cfoutput>" checked="checked" /> <cfoutput>#rs_grade.grade#</cfoutput>级 &nbsp;&nbsp;
								</cfloop>
							</div>

							<div id="notFullInstitute" class="secondRow hidden_elem mtm">
								指定学院
								<table>
									<tr>
										<cfset row = 0 />
										<cfset cellPerRow = 8 />
                                        <cfloop query="rs_institute">
											<cfset row++ />
												<td>
													<input name="targetInstitute" type="checkbox" class="typeCheckbox" <cfif row eq 1>verification="group"</cfif> value="<cfoutput>#rs_institute.institute_id#</cfoutput>" checked="checked" />
													<cfoutput>#rs_institute.institute_short#</cfoutput>
												</td>
											<cfif row mod cellPerRow eq 0>
									</tr>
									<tr>
											</cfif>
										</cfloop>
										<cfif row eq rs_institute.recordCount and rs_institute.recordCount mod cellPerRow>
											<cfloop from="1" to="#cellPerRow-rs_institute.recordCount mod cellPerRow#" index="i">
												<td><!-- <cfoutput>holder<cfoutput>#i#</cfoutput> --></cfoutput></td>
											</cfloop>
										</cfif>
									</tr>
								</table>
							</div>
							
							<div id="notFullGender" class="secondRow hidden_elem mtm">
								指定性别
								<input name="targetGender" type="checkbox" class="typeCheckbox" verification="group" value="1" checked="checked" /> 男生
								<input name="targetGender" type="checkbox" class="typeCheckbox" value="2" checked="checked" /> 女生
							</div>
							
							<div id="toSpecialStudent" class="hidden_elem mtm">
								<textarea name="StudentIDs" id="StudentIDs" verification="required"><cfoutput>#event.getArg('StudentIDs','NONE')#</cfoutput></textarea>
								<p class="notice">请在这里填写要指定参与投票学生的学号, 多个学号使用 , 或 换行 进行分割</p>
							</div>
							
						</div>
						
						<hr/>
						
						<div class="lable">
							<input class="button" type="submit" value="新建问卷" />
							<input class="button2" type="reset" value="取消" />
						</div>
						
					</form>
					
					<script language="javascript" type="text/javascript">
						//<![CDATA[
						<!--
						$(document).ready(function() {
							
							$("#DateStart").mask("9999-99-99");
							$("#DateEnd").mask("9999-99-99");
							
							Calendar.setup({
									trigger    : "startTrigger",
									inputField : "DateStart",
									animation  : false
									});
								
							Calendar.setup({
									trigger    : "endTrigger",
									inputField : "DateEnd",
									animation  : false
									});
									
							$("#FullGrade").change(function(){
								
								var checked = $(this).prop("checked");
									
								if ( checked ) {
									$("#notFullGrade").addClass("hidden_elem");
									$("#notFullGrade :checkbox[name='targetGrade']").attr("checked","checked");
								}
								else {
									$("#notFullGrade").removeClass("hidden_elem");
									$("#notFullGrade :checkbox[name='targetGrade']").removeAttr("checked");
								}
								
							});
							
							$("#FullInstitute").change(function(){
								
								var checked = $(this).prop("checked");
									
								if ( checked ) {
									$("#notFullInstitute").addClass("hidden_elem");
									$("#notFullInstitute :checkbox[name='targetInstitute']").attr("checked","checked");
								}
								else {
									$("#notFullInstitute").removeClass("hidden_elem");
									$("#notFullInstitute :checkbox[name='targetInstitute']").removeAttr("checked");
								}
							});
							
							$("#FullGender").change(function(){
								
								var checked = $(this).prop("checked");
									
								if ( checked ) {
									$("#notFullGender").addClass("hidden_elem");
									$("#notFullGender :checkbox[name='targetGender']").attr("checked","checked");
								}
								else {
									$("#notFullGender").removeClass("hidden_elem");
									$("#notFullGender :checkbox[name='targetGender']").removeAttr("checked");
								}
							});
							
							
							$("#SpecialStudent").change(function(){
								
								var checked = $(this).prop("checked");
								
								if ( checked ) {
									$("#toSpecialStudent").removeClass("hidden_elem");
									$("#toSpecialStudent textarea").val("");
								}
								else {
									$("#toSpecialStudent").addClass("hidden_elem");
									$("#toSpecialStudent textarea").val("NONE");
								}
								
							});

						});
						// -->
						//]]>
					</script>
					
				</div>
			
			</div>
		</li>
	</ul>
</div>